package test.base.medium;

import java.io.*;

public class ffmpegexe2 {
    protected String ffmpegApp;
    public ffmpegexe2(String ffmpegApp) {
        this.ffmpegApp = ffmpegApp;
    }
    @SuppressWarnings("unused")
    /****
     * 获取指定时间内的图片
     * @param videoFilename:视频路径
     * @param thumbFilename:图片保存路径
     * @param width:图片长
     * @param height:图片宽
     * @param hour:指定时
     * @param min:指定分
     * @param sec:指定秒
     * @throws IOException
     * @throws InterruptedException
     */
    public void getThumb(String videoFilename, String thumbFilename,  int hour, int min, float sec) throws IOException, InterruptedException {
            ProcessBuilder processBuilder = new ProcessBuilder(ffmpegApp, "-y", "-i", videoFilename, "-vframes", "1", "-ss", hour + ":" + min + ":" + sec, "-f", "mjpeg",  "-an",
                    thumbFilename);
        Process process = processBuilder.start();
        InputStream stderr = process.getErrorStream();
        InputStreamReader isr = new InputStreamReader(stderr);
        BufferedReader br = new BufferedReader(isr);
        String line;
        while ((line = br.readLine()) != null)
            ;
        process.waitFor();   
        if (br != null)
            br.close();
        if (isr != null)
            isr.close();
        if (stderr != null)
            stderr.close();
    }
    public static void main(String[] args) {
        String path = "D:\\config\\ffmpeg-win64\\bin\\ffmpeg.exe";

        ffmpegexe2 videoThumbTaker = new ffmpegexe2(path);
        try {
            videoThumbTaker.getThumb("D:\\test2.mp4", "D:\\test\\result.png", 0, 0, 1);
            File ffmpegFile = new File("D:\\test\\result.png");
            FileInputStream input=new FileInputStream(ffmpegFile);
            
            System.out.println("over");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}